<template>
	<view class="content">
		<u-navbar title="收货方式" :autoBack="true">
		</u-navbar>
		<view class="info">
			<view class="c1">
				<u-radio-group placement="row" v-model="info.take_over_way">
					<u-radio activeColor="#7677EB" :name="1" label="邮寄"></u-radio>
					<view style="margin-left: 40rpx;">
						<u-radio activeColor="#7677EB" :name="2" label="无需物流"></u-radio>
					</view>
				</u-radio-group>
			</view>

			<view class="c1">收货人</view>
			<view class="c2">
				<u--input v-model='info.take_over_name' placeholder="请输入" :customStyle="{padding: '0 32rpx'}">
				</u--input>
			</view>

			<view class="c1">手机号</view>
			<view class="c2">
				<u--input v-model='info.take_over_tel' placeholder="请输入" :customStyle="{padding: '0 32rpx'}">
				</u--input>
			</view>
			<template v-if="info.take_over_way !=2">
				<view class="c1">所在地区</view>
				<view class="c2">
					<u--input v-model='info.take_over_district' placeholder="请选择" :customStyle="{padding: '0 32rpx'}"
						suffixIcon="arrow-down" suffixIconStyle="color: #7D8592" readonly>
					</u--input>
					<view class="on_in" @click="addressShow = true"></view>
				</view>

				<view class="c1">详细地址</view>
				<view class="c2">
					<u--input v-model='info.take_over_address' placeholder="请输入" :customStyle="{padding: '0 32rpx'}">
					</u--input>
				</view>
			</template>

		</view>

		<view class="but">
			<view class="s2" @click="onSend()">
				<text>保 存</text>
			</view>
		</view>

		<address-picker :show="addressShow" closeOnClickOverlay @confirm='confirmAddress' @cancel='addressShow=false'
			@close='addressShow = false' :address-data="addressData" :indexs="indexs" :areaId="areaId"
			:type="type"></address-picker>

	</view>
</template>

<script>
	import conf from '@/common/config.js';
	export default {
		data() {
			return {
				info: {
					id: '',
					take_over_way: '',
					take_over_name: '',
					take_over_tel: '',
					take_over_district: '',
					take_over_address: ''
				},
				value: 1,
				addressShow: false,
				areaId: [1, 110000, 110101],
				addressData: ['北京市', '北京市', '东城区'],
				type: 3, //1-省，2-省市，3-省市区
				formType: '',
				indexs: [],
				id: '',
				form: {
					take_over_address: '',
					take_over_address_arer: ''
				},
				aid: '',
				id: '',
				types: ''
			};
		},
		onLoad({
			id,
			aid,
			type
		}) {
			this.info.id = id || aid
			this.aid = aid
			this.id = id
			this.types = type
		},
		onShow() {
			if (this.info.id && this.types !== 'money') {
				this.getInfo();
			}
			if (this.aid && this.types == 'money') {
				this.getInfoOrder();
			}
		},
		methods: {
			getInfoOrder() {
				this.$API.getOrderByOrderNo({
					order_no: this.aid
				}).then(res => {
					const {
						id,
						take_over_address,
						take_over_name,
						take_over_tel,
						take_over_way,
						take_over_district
					} = res.data
					this.info = Object.assign(this.info, {
						id,
						take_over_address,
						take_over_name,
						take_over_tel,
						take_over_way,
						take_over_district
					})
				});
			},
			getInfo() {
				this.$API.getUserInfo({
					id: this.info.id
				}).then(res => {
					const {
						take_over_address,
						take_over_name,
						take_over_tel,
						take_over_way,
						take_over_district
					} = res.data
					this.info = Object.assign(this.info, {
						take_over_address,
						take_over_name,
						take_over_tel,
						take_over_way,
						take_over_district
					})
					// this.form.take_over_address_arer = take_over_address && take_over_address.split(' ')[0] || ''
					// this.form.take_over_address = take_over_address && take_over_address.split(' ').slice(1) +
					// 	'' || ''
					// console.log(this.info);
				});
			},
			confirmAddress(val) {
				this.info.take_over_district = val.value.join('')
				this.addressShow = false
			},
			onClose() {
				this.addressShow = false;
			},
			onSend() {
				if (!this.info.take_over_way) return this.$u.toast('请选择收货方式')
				if (!this.info.take_over_name) return this.$u.toast('请输入收货人')
				if (!this.info.take_over_tel) return this.$u.toast('请输入手机号')
				if (this.info.take_over_way == 1 && !this.info.take_over_district) return this.$u.toast('请输入所在地区')
				if (this.info.take_over_way == 1 && !this.info.take_over_address) return this.$u.toast('请输入详情地址')
				try{
					this.$API.sendOrderTake({
						...this.info,
					}).then(res => {
						console.log(res);
						if (res.code == 1) {
							uni.showModal({
								content: '保存成功',
								showCancel: false,
								success: () => {
									if (this.aid) {
										return uni.redirectTo({
											url: '/pages/my/activity'
										})
									}
									uni.navigateBack()
								}
							})
						} else {
							this.$u.toast(res.msg)
						}
					
					})
				}catch(e){
					console.log(e,'error');
					//TODO handle the exception
				}
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 670rpx;
		margin: 0 auto;
	}

	.info {
		width: 566rpx;
		padding: 0 52rpx 52rpx 52rpx;
		margin: 52rpx auto;
		background-color: #fff;
		border-radius: 48rpx;
		@include flex(column);

		.avatar {
			image {
				width: 120rpx;
				height: 120rpx;
				border-radius: 50%;
			}
		}

		.c1 {
			height: 28rpx;
			font-size: 28rpx;
			font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
			font-weight: 400;
			color: #7D8592;
			line-height: 28rpx;
			margin-top: 56rpx;
		}

		.c2 {
			width: 566rpx;
			margin-top: 40rpx;
			position: relative;

			/deep/ .u-input {
				height: 98rpx;
				border-radius: 28rpx;
			}

			.on_in {
				position: absolute;
				inset: 0;
			}
		}

		.qrcode {
			image {
				width: 128rpx;
				height: 128rpx;
				margin-top: 24rpx;
			}
		}

	}

	.but {
		@include flex;
		margin-top: 60rpx;
		margin-bottom: 60rpx;

		.s2 {
			width: 290rpx;
			height: 96rpx;
			background: #7677EB;
			box-shadow: 0 12rpx 24rpx 0 rgba(63, 140, 255, 0.2637);
			border-radius: 28rpx;
			margin: 0 auto;
			text-align: center;

			text {
				font-size: 30rpx;
				font-family: Alibaba PuHuiTi 2.0, Alibaba PuHuiTi 20;
				font-weight: 400;
				color: #FFFFFF;
				line-height: 96rpx;
			}
		}
	}
</style>